import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../theme/app_colors.dart';
import '../../../theme/app_text_theme.dart';
import 'h5_controller.dart';
import 'package:webview_flutter/webview_flutter.dart';


class H5View extends GetView<H5Controller> {
  const H5View({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if(!GetPlatform.isAndroid && !GetPlatform.isIOS) {
      return const Center(child: Text('暂不支持'));
    }
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(title: Text(controller.title,style: text_131732_18b)),
      body: WillPopScope(
        onWillPop: () async {
          var canGoBack = await controller.webViewController?.canGoBack();
          if(canGoBack ?? true){
            controller.webViewController?.goBack();
            return false;
          }else{
            return true;
          }
        },
        child: WebView(
          initialUrl: controller.url,// 初始化 URL
          onWebViewCreated: (WebViewController webViewController) { // WebView 创建完成之后的回调
            controller.webViewController = webViewController;
          },
          javascriptMode: JavascriptMode.unrestricted, // JS执行模式 默认是不调用
          backgroundColor: Colors.transparent,
          javascriptChannels: <JavascriptChannel>[
            _toastJsChannel(context),
          ].toSet(),
        ),
      ),
    );
  }

// 创建 JavascriptChannel
  JavascriptChannel _toastJsChannel(BuildContext context) => JavascriptChannel(
      name: 'show_flutter_toast',
      onMessageReceived: (JavascriptMessage message) {
        print("get message from JS, message is: ${message.message}");
  });

}
